// NitroFileReader.cs:
// Reader for Nitro (Nintendo DS) files.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License.
// See LICENSE for more information.

using System.IO;

namespace SharpNitroLib.NARC
{
	/// <summary>
	/// Reader for Nitro (Nintendo DS) files.
	/// </summary>
    public class NitroFileReader
    {
    	/// <summary>
    	/// Binary reader of source.
    	/// </summary>
        private BinaryReader _reader;

        /// <summary>
        /// Create NitroFileReader.
        /// </summary>
        /// <param name="stream">Stream to use.</param>
        public NitroFileReader(Stream stream) : this(new BinaryReader(stream)){}

        /// <summary>
        /// Create NitroFileReader.
        /// </summary>
        /// <param name="reader">Binary reader to use.</param>
        public NitroFileReader(BinaryReader reader)
        {
            _reader = reader;
        }

        /// <summary>
        /// Read NitroFile.
        /// </summary>
        /// <returns>NitroFile read from source.</returns>
        public NitroFile Read()
        {
            var nitroFile = new NitroFile();
            var header = NitroFileHeader.Read(_reader);
            nitroFile.FileType = header.FileType;
            for (var i = 0; i < header.NumFrames; ++i)
            {
                var frame = new NitroFileFrameReader(_reader).Read();
                nitroFile.AddFrame(frame);
            }
            return nitroFile;
        }
    }
}